﻿define(["jquery"], function ($) {
    var antiSqlValid = function(oField) {
        re = /"exec|insert|select|update|delete|master|truncate|char|declare|join|iframe|href|script|exec|count|request|’|--|"|=|;|>|<|%/i;
        if (re.test(oField)) {
            return true;
        }
    }
    Date.prototype.diff = function (date) {
        return (this.getTime() - date.getTime()) / (24 * 60 * 60 * 1000);
    }
    function dateDiff(startdate,enddate) {
        var sdate = new Date(startdate);
        var edate = new Date(enddate);
        if (edate.diff(sdate) <= 29)
            return false;
        return true;
    }
    return {
        checkSqlInject: function () {
            var injectflag = true;
            $("input[type=text]").each(function () {
                if (antiSqlValid($(this).val())) {
                    injectflag = false;
                    alert('参数含有非法字符，访问已被拒绝!');
                    return false;
                }
            });
            return injectflag;
        },
        checkDate: function (str) {
            var startTime = $("#StartTime").val();
            var endTime = $("#EndTime").val();
            if (startTime || startTime == "") {
                if (startTime == "") {
                    alert(str + "开始时间不可为空！");
                    return false;
                }
                if (endTime == "") {
                    alert(str + "结束时间不可为空！");
                    return false;
                }
                if (startTime > endTime) {
                    alert(str + "时间范围不正确！");
                    return false;
                }
                //判断是否超过限制，默认6个月
                var month = 6;
                var capitalMonth = "六";
                var errorMessage = "您选择的日期已超过最大有效日期，请选择近" + capitalMonth + "个月内的日期！"
                var d = new Date();
                if (window.location.href.indexOf("/UserPoint/") > -1 || window.location.href.indexOf("/PointExchange/") > -1) {
                    var month = 1;
                    var capitalMonth = "一";
                    errorMessage = "您选择的时间区间最大跨度不能超过" + capitalMonth + "个月！";
                    d = new Date(endTime.replace(/-/g, "/"));
                }

                d.setMonth(d.getMonth() - month);
                if (new Date(startTime.replace(/-/g, "/")) < d) {
                    alert(errorMessage);
                    return false;
                }
            }
            return true;
        },
        isPlaceholer: function () {
            //取得浏览器的userAgent字符串
            var userAgent = navigator.userAgent;
            //判断是否IE浏览器
            if (userAgent.indexOf("MSIE") > -1) {
                //判断是否支持placeholder
                var supportPlaceholder = 'placeholder' in document.createElement('input');
                return supportPlaceholder
            }
            return true;
        },
        triggerCreateTabEvent: function (id, title, url) {
            var $createTab = $(window.parent.document).find('#createTab');
            $createTab.attr("data-id", id).attr("data-title", title).attr("data-url", url);
            $createTab.click();
        }
    };
});